using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Microsoft.Practices.Unity;
using Silverlight.Weblog.Common.IoC;
using Silverlight.Weblog.Server.DAL;
using Silverlight.Weblog.Server.DAL.Services;
using Silverlight.Weblog.Server.DAL.Services.Interfaces;

namespace Silverlight.Weblog.UI.Web.Controllers
{
    public class ResolvedBlogPostList : IResolvedBlogRoute
    {
        public ResolvedBlogPostList()
        {
            IoC.BuildUp(this);
        }

        [Dependency]
        public IUserService UserService { get; set; }

        [Dependency]
        public IBlogPostService BlogPostService { get; set; }

        [Dependency]
        public ICategoryService CategoryService { get; set; }

        public ResolvedBlogPostList(string Permlink, string Username, Dictionary<string, string> FragmentQueryString)
            : this()
        {
            if (string.IsNullOrEmpty(Username)
             && !string.IsNullOrEmpty(Permlink)
             && !FragmentQueryString.Any())
            {
                ResolveAsUserBlogHomePage(Permlink);
            }
            else if (string.IsNullOrEmpty(Username)
                  && string.IsNullOrEmpty(Permlink)
                && !FragmentQueryString.Any())
            {
                ResolveAsBlogHomePage();
            }
            else if (FragmentQueryString.Any())
            {
                ResolveNavigationFragment(FragmentQueryString);
            }
            else
            {
#if DEBUG
                throw new Exception(Permlink + " " + Username + " are not supported for blog post resolving.");
#else
                _title = "Could not resolve: " + Permlink + " " + Username;
#endif
            }
        }

        private List<BlogPost> _blogPosts = new List<BlogPost>();
        private string _title = null;

        private void ResolveAsUserBlogHomePage(string str1)
        {
            var blogPosts = BlogPostService.GetUserByUserNameWithBlogPosts(str1);

            _blogPosts = blogPosts;

            if (_blogPosts != null && blogPosts.Any())
                _title = blogPosts[0].User.BlogName;
        }

        private void ResolveAsBlogHomePage()
        {
            _blogPosts = this.BlogPostService.GetHomePageBlogPosts();

            User FirstDefaultUser = _blogPosts.Select(b => b.User).FirstOrDefault(u => u.IsDefaultBlog.HasValue && u.IsDefaultBlog.Value);
            if (FirstDefaultUser != null)
                _title = FirstDefaultUser.BlogName;
            else
                _title = "Home Page";
        }

        private void ResolveNavigationFragment(Dictionary<string, string> FragmentQuerystring)
        {
            if (FragmentQuerystring.ContainsKey("Home"))
            {
                ResolveAsUserBlogHomePage(FragmentQuerystring["Home"]);
            }
            if (FragmentQuerystring.ContainsKey("BlogPost"))
            {
                ResolveAsUserBlogHomePage(FragmentQuerystring["Home"]);
            }
            if (FragmentQuerystring.ContainsKey("Search"))
            {
                ResolveAsSearchPage(FragmentQuerystring["Search"], FragmentQuerystring["Username"]);
            }
            if (FragmentQuerystring.ContainsKey("Category"))
            {
                ResolveAsCategoryPage(FragmentQuerystring["Category"], FragmentQuerystring["Username"]);
            }
        }

        private void ResolveAsCategoryPage(string category, string username)
        {
            _blogPosts = BlogPostService.GetBlogPostsInCategory(category, username);
            _title = "Category: " + category;
        }

        private void ResolveAsSearchPage(string SearchTerm, string Username)
        {
            _blogPosts = BlogPostService.SearchFor(SearchTerm, Username);
            _title = "Search: " + SearchTerm;
        }

        #region Implementation of IResolvedBlogRoute

        public ActionResult ResolveActionResult(BlogPostController controller)
        {
            return controller.PublicView("BlogPostView", new BlogPostDisplayData()
                                                             {
                                                                 CollectionBlogPosts = _blogPosts,
                                                                 Title = _title
                                                             });
        }

        public IEnumerable<BlogPost> GetRelevantBlogPosts()
        {
            return _blogPosts;
        }

        #endregion
    }
}